package com.sui.bigdata.rtcadmin.util;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author YongChen
 * @date 2020/4/17 9:47
 * @description
 * @email yong_chen@sui.com
 */
public class MetricUtils {
    protected static final Logger logger = LoggerFactory.getLogger(MetricUtils.class);

    public static void wipeMetricData(String url) {
        try {
            for (int i = 0; i < 3; i++) {
                try {
                    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
                    HttpPut httpput = new HttpPut(url);
                    httpput.setConfig(RequestConfig.custom().setConnectTimeout(10000).build());
                    httpput.setHeader("Content-Type", "text/plain; version=0.0.4; charset=utf-8");
                    CloseableHttpResponse response = closeableHttpClient.execute(httpput);
                    if (202 == response.getStatusLine().getStatusCode()) {
                        break;
                    }
                } catch (Exception e) {
                    if (i == 2) {
                        throw e;
                    }
                    Thread.sleep(200);
                }
            }
        } catch (Exception e) {
            logger.error(" clean metric failed : ", e);
        }
    }
}
